﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using Project5188.BUS;
using Project5188.BUS.Operations;
using ExportToExcelTools;
using Project5188.Client.ClientCodes;

namespace Project5188.Client.Operations
{
    /// <summary>
    /// Interaction logic for BranchScreenPage.xaml
    /// </summary>
    public partial class BranchPage
    {
        private CollectionViewSource _resources;

        public BranchPage()
            : base(Extensions.PageNames.BranchPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            BindData();
        }

        private void BindData()
        {
            var parameterValues = new ParameterValues();

            drpWorkingType.SelectedValuePath = "ID";
            drpWorkingType.IsSynchronizedWithCurrentItem = true;
            drpWorkingType.ItemTemplate = Resources["comboTemplate"] as DataTemplate;
            drpWorkingType.ItemsSource = parameterValues.GetEntityByParameterId(2);

            drpPoliceForceArea.SelectedValuePath = "ID";
            drpPoliceForceArea.IsSynchronizedWithCurrentItem = true;
            drpPoliceForceArea.ItemTemplate = Resources["comboTemplate"] as DataTemplate;
            drpPoliceForceArea.ItemsSource = parameterValues.GetEntityByParameterId(3);
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgBranches.ItemsSource = _resources.Source as IEnumerable;
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var branches = new Branches();
                _resources = Resources["branchesViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    branches.GetEntityListByParameters(txtBranchName.Text, Convert.ToInt64(drpWorkingType.SelectedValue),
                                                       Convert.ToInt64(drpPoliceForceArea.SelectedValue)), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgBranches.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var branches = new Branches();
                _resources = Resources["branchesViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    branches.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgBranches.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var branchValue = (branch)dgBranches.SelectedItem;
            var detail = new DetailWindow();
            DetailView<branch> branchDetail = new Branches { Entity = branchValue };
            detail.SetEntity(branchDetail, Extensions.PageNames.BranchPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var branchValue = (branch)dgBranches.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<branch> branchDetail = new Branches();
            update.SetEntity(branchDetail, Extensions.PageNames.BranchPage.ToString(), branchValue.a_ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var branchValue = (branch)dgBranches.SelectedItem;
            DetailView<branch> branchDetail = new Branches();
            GenerateDeleteDialog(branchDetail, branchValue.a_ID);
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<branch> branchDetail = new Branches();
            insert.SetEntity(branchDetail, Extensions.PageNames.BranchPage.ToString());
            insert.Show();
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgBranches.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }
    }
}
